<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
$user_id = base64_decode($_GET['id']);
$user = Table::Fetch('user', $user_id);
file_put_contents('url.txt', $_SERVER['HTTP_REFERER']);

if($user && $user['enable'] == 'Y' && $user['emailable'] == 'Y'){ //如果存在此注册用户，并且通过验证，才往下执行
	/////* 初次接入验证 */
	if($_GET['echostr'] && $_GET['signature']){
		$verify = array(
			'token' => $user['token'],
			'signature' => $_GET["signature"],
			'timestamp' => $_GET["timestamp"],
			'nonce' => $_GET["nonce"],
		);
		$vrs = ZWeixin::Verify($verify);
		if($vrs === true){
			echo $_GET["echostr"];
		}
	}
	/////* 初次接入验证 end */
	
	$xml = $GLOBALS["HTTP_RAW_POST_DATA"]; //post数据
	if (!empty($xml)){
		$postObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
		$msgType = strval($postObj->MsgType); //MsgType消息类型（text文本，image图片，location地理位置，link链接，event事件）
		$event = strval($postObj->Event); //Event事件类型，subscribe(订阅)、unsubscribe(取消订阅)、CLICK(自定义菜单点击事件)
		$res =  ZWeixin::Run($user, $msgType, $postObj, $event);
		header('Content-Type: application/xml; charset=UTF-8');
		print_r($res);
	}
	
}
















